<?php
namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\controllers\BaseController;
use app\common\Deal;
use app\models\GameUser;
use app\models\Shop;
use app\models\ShopRecord;
use app\models\Morning;

class ShopController extends BaseController
{
     public $enableCsrfValidation = false;
    //商品展示功能
     public function actionDisplay(){             
          if(Yii::$app->request->isPost){
             $data = Shop::find()->where('isShow =:isShow',[':isShow' => '1'])->asArray()->orderBy('score ASC,settime DESC')->all();
             return BaseController::SuccessRespone($data);
             exit(0);
         }
   }

     //兑换商品功能
      public function actionBuycoupon(){
           $redis = Yii::$app->redis;
           $deal = new Deal();
           if(Yii::$app->request->isPost){
              $postData = Yii::$app->request->post();
              $session = $postData['session'];
              $commodityId = $postData['cid'];
              $name = $postData['name'];
              $score = $postData['score'];
              $key = '_3rd_session:'.$session;
              $result = json_decode($redis->get($key),true);
              $openid = $result['openid']; 
             //获取用户序号
              $user_data = $this->judgeSession($session);
              $userid = $user_data['id'];
              //获取用户商城积分
              $scoredata = Morning::data_by_id($userid);
              $shopscore = $scoredata['shopscore'];
               //获取商品信息
              $shopdata = Shop::judgeId($commodityId);
              $count = $shopdata['count'];
              $image = $shopdata['image'];
              $soldoutTime = $shopdata['soldoutTime']; 
              $recorddata = array(
                 'cid' =>$commodityId,
                 'openid' => $openid,
                );
            
              $buyed = ShopRecord::judgeOpenCid($recorddata);
              //判断用户是否已经兑换过改商品了
              if($buyed['openid'] == $openid && $buyed['commodityId'] == $commodityId)
              {
                  $data = "对不起，您已兑换过该商品~";
                  return BaseController::SuccessRespone($data);
                  exit(0);  
               }
              //判断现有的积分知不知够买？然后再更新数据库
              if ( $shopscore >= $score ){
 
               $userdata = array(
                'commodityId' => $commodityId,
                'image' => $image,
                'openid' => $openid,
                'name' => $name,
                'soldoutTime' => $soldoutTime,
               ); 
               $newscore = $shopscore-$score;              
               $coupondata = array(
                      'userid' => $userid,
                      'newscore' => $newscore,
                );
              if($count > 0){
              //更新数据库
              $result = Morning::scoreUpdate($coupondata);
              if( $result == 1){
                $newcount = $count-1;
                $shopdata = array(
                     'id' => $commodityId,
                     'count' => $newcount,
                );
                $data = Shop::countUpdate($shopdata);
                $code = ShopRecord::add($userdata);
                 
                if( $code == 1 && $data == 1){
                
                $data = "兑换成功";
                return BaseController::SuccessRespone($data);
                 exit(0);
                }else if( $code == -1 || $data == -1){
                  $content = "兑换失败,请联系客服";
                  //数据库录入失败
                  $data = array(
                    '0' => -1,
                    '1' => $content,
                  );
                return BaseController::FailRespone($data);
                exit(0);
                }
              }else if( $result == -1 ){
                 $content = "兑换失败,请联系客服";
                //数据库录入失败
                $data = array(
                    '0' => -1,
                    '1' => $content,
                );
                return BaseController::FailRespone($data);
                exit(0);
                } 
             }else{
                $content = "对不起，该商品库存不足";
                $data = array(
                    '0' => -1,
                    '1' => $content,
                 );
                 return BaseController::FailRespone($data);  
            }
           }else{
                $content = "对不起，您的积分不够";
                $data = array(
                    '0' => -1,
                    '1' => $content,
                 );
                 return BaseController::FailRespone($data);
                    
             } 
           }else{
            $content = "接收的posts数据为空";
            $data = array(
                '0' => -2,
                '1' => $content,
            );
            return BaseController::FailRespone($data);
        }              

              exit(0);                
       }
        
       //用户优惠券页面展示
       public function actionCoupon(){
           $coupon = new ShopRecord();
           $redis = Yii::$app->redis;
           $deal = new Deal();
          if(Yii::$app->request->isPost){
              $postData = Yii::$app->request->post();
              $session = $postData['session'];
              $key = '_3rd_session:'.$session;
              $result = json_decode($redis->get($key),true);
              $openid = $result['openid'];
              $sql = "select * from blxb_shop_record where openid = '$openid'  order by state desc,transTime desc";
              $data = Shop::findBysql($sql)->asArray()->all();
              return BaseController::SuccessRespone($data);
            exit(0);
         }
 }

        //用户使用优惠券功能
       public function actionUsecoupon(){
           $redis = Yii::$app->redis;
           $deal = new Deal(); 
          if(Yii::$app->request->isPost){
              $postData = Yii::$app->request->post();
              $session = $postData['session'];
              $key = '_3rd_session:'.$session;
              $result = json_decode($redis->get($key),true);
              $openid = $result['openid'];
              $id = $postData['id'];
              $coupondata = array(
                  'id' => $id,
                  'openid' => $openid,
               );
              $result = ShopRecord::stateUpdate($coupondata);
              if( $result == 1){
                  $data = "使用成功";
                  return BaseController::SuccessRespone($data);
                  exit(0);
               }else if( $result == -1){
                  $content = "使用失败";
                  $data = array(
                    '0' => -1,
                    '1' => $content,
                );
                return BaseController::FailRespone($data);
                exit(0);
            }
        }else{
            $content = "接收的posts数据为空";
            $data = array(
                '0' => -2,
                '1' => $content,
            );
            return BaseController::FailRespone($data);
        }
              exit(0);
       }

}

